#!/home/disk/pvort/lmadaus/nobackup/pylibs/epd/bin/python

# Script to update the date on the WX Challenge page
import os
import pickle
from datetime import datetime,timedelta
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-m','--message',action='store',dest='msg',default='',type='string',help='Message to display about update')
parser.add_option('-i','--high',action='store',dest='high',default='',type='string',help='Latest high temperature')
parser.add_option('-l','--low',action='store',dest='low',default='',type='string',help='Latest low temperature')
parser.add_option('-w','--wind',action='store',dest='wind',default='',type='string',help='Latest max wind')
parser.add_option('-p','--precip',action='store',dest='precip',default='',type='string',help='Latest precip')

from INFILE import header_title, webdir, siteid

(opts,args) = parser.parse_args()
message = opts.msg
high = opts.high
low = opts.low
wind = opts.wind
precip = opts.precip

print message


# Need to update the times in the script

now = datetime.utcnow()
now_local = datetime.now()
next_fcst_time = now + timedelta(hours=24)
fmt_next_fcst_date = next_fcst_time.strftime('%m/%d/%Y')

next_fcst_time = datetime.strptime(fmt_next_fcst_date,'%m/%d/%Y')
fmt_next_fcst_time = next_fcst_time.strftime('"%m/%d/%Y %I:%M %p UTC"')


outfile = open('index.html','w')

print >> outfile,"""<html>
<head>
<title>WxChallenge Tools</title>
<link rel="stylesheet" href="wxchall.css" type="text/css" media="all">

<script language="JavaScript" type="text/JavaScript">
function showT(imgname){
   //var obj = top.image_frm.document.images['ima'];
   var obj = document.images['ima'];
   var src = obj.src;
   var pos = src.indexOf('?');
   if (pos >= 0) {
      src = src.substr(0, pos);
   }
   var date = new Date();

"""
print >>outfile,"   obj.src = '%s_'+ imgname + '?v=' + date.getTime();" % siteid.upper()

print >>outfile,"""
   return false;
}

</script>




</head>
<body>

<div id="top_title"> 
	<div id="maintitle">"""
print >>outfile,"UW WxChallenge Tools -- %s</b>&nbsp&nbsp&nbsp&nbsp&nbsp" % header_title
print >>outfile,"""
		<font size="3" color="blue"><i>(A Theta-E Production)</i></b></font><br/>
	</div>
	<div id="todaysofar">
"""
print >>outfile,"		Today so far:&nbsp;&nbsp;High: %s&deg;F&nbsp;&nbsp;&nbsp;&nbsp;Low: %s&deg;F&nbsp;&nbsp;&nbsp;&nbsp;Wind: %s kts&nbsp;&nbsp;&nbsp;&nbsp;Precip: %s" % (high,low,wind,precip)
print>>outfile,"""
	</div>
</div>

<div id="dateinfo">
"""
print >>outfile,'     <font size="3"><b>Forecast date: %s </b></font><br/> ' % fmt_next_fcst_date 
print >>outfile,'     <font color="Goldenrod">Page last update: %s<br/>' % now_local.strftime('%m/%d/%Y %I:%M %p PST')
#print >>outfile,"=====> %s</font><br/>" % message
print >>outfile,"     <b>Countdown to next 00Z fcst. due time:</b><br>"
print >>outfile,'     <script language="JavaScript">'
print >>outfile,'         TargetDate = %s;' %fmt_next_fcst_time
print >>outfile,"""         BackColor = "#505080";
         ForeColor = "yellow";
         CountActive = true;
         CountStepper = -1;
         LeadingZero = true;
         DisplayFormat = "%%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
         FinishMessage = "Forecast window has closed!";
     </script>
     <script language="JavaScript" src="countdown.js"></script>

</div>
<div id="sidebar">
     <table class="gridtable">
     <tr><th colspan="4"><div id="suptitle"> Temperatures </div></th></tr>
     <tr><td colspan="4"><a href="" onclick="showT('temp.png');return false;">Latest Model Summary</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('temp_sref.png');return false;">Latest SREF Forecast</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('maxt.png');return false;">High Temp Climatology</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('mint.png');return false;">Low Temp Climatology</a></td></tr>
     <tr><td colspan="2">GFS Ensemble Temps
     <a href="gfse_help.html" onclick="window.open('gfse_help.html','popup','width=600,height=700,scrollbars=yes,resizable=no, 
         toolbar=no,directories=no,location=no,menubar=no,status=no');return false"> ? </a>

     </td><td><a href="#" onclick="showT('GFSE_highs.png');return false;">&nbsp;High</a></td>
     <td><a href="#" onclick="showT('GFSE_lows.png');return false;">&nbsp;Low</a></td></tr>

     <tr><th colspan="4"><div id="subtitle">Time-Height Plots</div></th></tr>
     <tr><td>Temperature</td><td><a href="" onclick="showT('GFS_TMPC_tmht.png');return false;">GFS</a></td>
     <td><a href="" onclick="showT('NAM_TMPC_tmht.png');return false;">NAM</a></td>
     <td><a href="" onclick="showT('NAMM_TMPC_tmht.png');return false;">NAMm</a><br></td></tr>
     <tr><td>Dewpt Depr.</td><td><a href="" onclick="showT('GFS_DWP-DEP_tmht.png');return false;">GFS</a></td>
     <td><a href="" onclick="showT('NAM_DWP-DEP_tmht.png');return false;">NAM</a></td>
     <td><a href="" onclick="showT('NAMM_DWP-DEP_tmht.png');return false;">NAMm</a><br></td></tr>
     <tr><td>Cloud Fraction</td><td><a href="" onclick="showT('SREF_CFRL_tmht.png');return false;">SREF</a></td>
     <td><a href="" onclick="showT('NAM_CFRL_tmht.png');return false;">NAM</a></td>
     <td><a href="" onclick="showT('NAMM_CFRL_tmht.png');return false;">NAMm</a><br></td></tr>

     <tr><th colspan="4"><div id="subtitle">High Temps Verification</div></th></tr>
     <tr><td>GFS 12Z MOS  </td><td><a href="" onclick="showT('GFS_high_10day.png');return false;">10 Day</a></td>
     <td><a href="" onclick="showT('GFS_high_30day.png');return false;"> 30 Day</a></td>
     <td><a href="" onclick="showT('GFS_high_60day.png');return false;"> 60 Day</a><br></td></tr>
     <tr><td>NAM 12Z MOS </td><td><a href="" onclick="showT('NAM_high_10day.png');return false;">10 Day</a></td>
     <td><a href="" onclick="showT('NAM_high_30day.png');return false;"> 30 Day</a></td>       
     <td><a href="" onclick="showT('NAM_high_60day.png');return false;"> 60 Day</a></td></tr> 
     <tr><td>NWS 12Z FCT </td><td colspan="3"><a href="" onclick="showT('NWS_high_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>USL 12Z RAW </td><td colspan="3"><a href="" onclick="showT('USL12Z_high_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>USL 22Z RAW </td><td colspan="3"><a href="" onclick="showT('USL22Z_high_10day.png');return false;">10 Day</a></td></tr>

     <tr><th colspan="4"><div id="subtitle">Low Temps Verification</div></th></tr>
     <tr><td>GFS 12Z MOS</td><td><a href="" onclick="showT('GFS_low_10day.png');return false;">10 Day</a></td>
     <td><a href="" onclick="showT('GFS_low_30day.png');return false;"> 30 Day</a></td>
     <td><a href="" onclick="showT('GFS_low_60day.png');return false;"> 60 Day</a></td></tr> 
     <tr><td>NAM 12Z MOS</td><td><a href="" onclick="showT('NAM_low_10day.png');return false;">10 Day</a></td>
     <td><a href="" onclick="showT('NAM_low_30day.png');return false;"> 30 Day</a></td>       
     <td><a href="" onclick="showT('NAM_low_60day.png');return false;"> 60 Day</a></td></tr> 
     <tr><td>NWS 12Z FCT</td><td colspan="3"><a href="" onclick="showT('NWS_low_10day.png');return false;">10 Day</td></tr>
     <tr><td>USL 12Z RAW</td><td colspan="3"><a href="" onclick="showT('USL12Z_low_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>USL 22Z RAW</td><td colspan="3"><a href="" onclick="showT('USL22Z_low_10day.png');return false;">10 Day</a></td></tr>
     </table>
     <br>
     <table class="gridtable">
     <tr><th colspan="4"><div id="suptitle"> Winds </div></th></tr>
     <tr><td colspan="4"><a href="" onclick="showT('wspd.png');return false;">Latest Model Summary</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('wspd_sref.png');return false;">Latest SREF Forecast</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('winddir.png');return false;">Latest Model Wind Directions</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('blwinds.png');return false;">Latest Mixed-Layer Winds Forecast</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('maxsp.png');return false;">Max Wind Climatology</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('windrose.png');return false;">Wind Rose</a></td></tr>

     <tr><th colspan="4"><div id="subtitle">Time-Height Plots</div></th></tr>
     <tr><td>Wind Speed</td><td><a href="" onclick="showT('GFS_SKNT_tmht.png');return false;">GFS</a></td>
     <td><a href="" onclick="showT('NAM_SKNT_tmht.png');return false;">NAM</a></td>
     <td><a href="" onclick="showT('NAMM_SKNT_tmht.png');return false;">NAMm</a><br></td></tr>

     <tr><th colspan="4"><div id="subtitle">Winds Verification</div></th></tr>
     <tr><td>GFS 12Z MOS</td><td><a href="" onclick="showT('GFS_wind_10day.png');return false;">10 Day</a></td>
     <td><a href="" onclick="showT('GFS_wind_30day.png');return false;"> 30 Day</a></td>       
     <td><a href="" onclick="showT('GFS_wind_60day.png');return false;"> 60 Day</a></td></tr> 
     <tr><td>NAM 12Z MOS</td><td><a href="" onclick="showT('NAM_wind_10day.png');return false;">10 Day</a></td>
     <td><a href="" onclick="showT('NAM_wind_30day.png');return false;"> 30 Day</a></td>       
     <td><a href="" onclick="showT('NAM_wind_60day.png');return false;"> 60 Day</a></td></tr>
     <tr><td>NWS 12Z FCT</td><td colspan="3"><a href="" onclick="showT('NWS_wind_10day.png');return false;">10 Day</td></tr>
     <tr><td>USL 12Z RAW</td><td colspan="3"><a href="" onclick="showT('USL12Z_wind_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>USL 22Z RAW</td><td colspan="3"><a href="" onclick="showT('USL22Z_wind_10day.png');return false;">10 Day</a></td></tr>
     </table>
     <br>
     <table class="gridtable">
     <tr><th colspan="4"><div id="suptitle"> Precipitation </div></th></tr>
     <tr><td colspan="4"><a href="" onclick="showT('precip.png');return false;">Latest Model Summary</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('precip_sref.png');return false;">Latest SREF Forecast</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('precip_climo.png');return false;">Precip Climatology</a></td></tr>
     <tr><td colspan="4"><a href="" onclick="showT('hpc_pctiles.png');return false;">HPC Precip Confidence Intervals</a>
     <a href="hpc_help.html" onclick="window.open('hpc_help.html','popup','width=600,height=700,scrollbars=yes,resizable=no, 
         toolbar=no,directories=no,location=no,menubar=no,status=no');return false"> ? </a></td></tr>
     <tr><td colspan="2">GFS Ensemble Precip</td><td><a href="#precip" onclick="showT('GFSE_pop.png');return false;">&nbsp;&nbsp;&nbsp;PoP&nbsp;&nbsp;</a></td>
     <td><a href="#precip" onclick="showT('GFSE_qpfc.png');return false;">&nbsp;&nbsp;&nbsp;QPF&nbsp;&nbsp;</a></td></tr>

     <tr><th colspan="4"><div id="subtitle">Time-Height Plots</div></th></tr>
     <tr><td>Omega</td><td><a href="" onclick="showT('GFS_OMEG_tmht.png');return false;">GFS</a></td>
     <td><a href="" onclick="showT('NAM_OMEG_tmht.png');return false;">NAM</a></td>
     <td><a href="" onclick="showT('NAMM_OMEG_tmht.png');return false;">NAMm</a><br></td></tr>

     <tr><th colspan="4"><div id="subtitle">Precip Verification</div></th></tr>
     <tr><td>GFS 12Z RAW</td><td><a href="" onclick="showT('GFS_precip_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>NAM 12Z RAW</td><td><a href="" onclick="showT('NAM_precip_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>HPC RAW</td><td><a href="" onclick="showT('HPC_precip_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>NWS 12Z FCT</td><td><a href="" onclick="showT('NWS_precip_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>USL 12Z RAW</td><td><a href="" onclick="showT('USL12Z_precip_10day.png');return false;">10 Day</a></td></tr>
     <tr><td>USL 22Z RAW</td><td><a href="" onclick="showT('USL22Z_precip_10day.png');return false;">10 Day</a></td></tr>
     </table>
</div>     


<div id="image_place">
    <img id="ima" src="">

</div>
"""
#     <tr><td>HRMOS QPF
#     <a href="hrmos_help.html" onclick="popup=window.open('hrmos_help.html','popup','width=600,height=700,scrollbars=yes,resizable=no, 
#         toolbar=no,directories=no,location=no,menubar=no,status=no');return false;"> ? </a>
#     </td><td><a href="" onclick="showT('HRMOS_precip_10day.png');return false;">10 Day</a></td></tr>
# Here we will define the image maps for the different information
# Use javascript to change out the maps
# Make a list of all coordinate maps
#os.listdir('/home/dik/pvort/lmadaus/nobackup




print >>outfile, """
</body>

</html>
"""
outfile.close()
os.system('mv index.html %s' % webdir)
